package kr.pe.johnkim.pushchat.mobile.controller;

import kr.pe.johnkim.pushchat.mobile.domain.PushChatAnnouncement;
import kr.pe.johnkim.pushchat.mobile.service.PushChatAnnouncementService;
import kr.pe.johnkim.pushchat.mobile.util.PushChatCommon;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author : John_Kim <cale9797@gmail.com>
 * @date : 13. 9. 13.
 */
@Controller
@RequestMapping(value = "/pushChatAnnouncementController")
public class PushChatAnnouncementController {

    // SLF4J Logging
    private Logger logger = LoggerFactory.getLogger(PushChatAnnouncementController.class);

    @Autowired
    private PushChatAnnouncementService pushChatAnnouncementService;

    @Autowired
    private PushChatCommon pushChatCommon;

    @Autowired
    private HttpServletRequest httpServletRequest;

    // 파일 업로드 Directory.
    private @Value("${pushchat.upload.announcement.file.directory}") String uploadAnnouncementFileDirectory;

    // 파일 업로드 URL.
    private @Value("${pushchat.upload.announcement.file.url}") String uplaodAnnouncementFileUrl;

    // 썸네일 업로드 Directory.
    private @Value("${pushchat.upload.announcement.thumbnail.directory}") String uploadAnnouncementThumbnailDirectory;

    // 썸네일 업로드 URL.
    private @Value("${pushchat.upload.announcement.thumbnail.url}") String uploadAnnouncementThumbnailUrl;

    /**
     * 공지사항을 등록 한다.
     *
     * @param pushChatAnnouncement
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/requestCertification", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public PushChatAnnouncement registerAnnouncement(@RequestBody final PushChatAnnouncement pushChatAnnouncement) throws Exception {
        logger.info("registerAnnouncement");

        PushChatAnnouncement result = new PushChatAnnouncement();

        try {
            this.pushChatAnnouncementService.registerAnnouncement(pushChatAnnouncement);
            result.setResult("SUCCESS");
            logger.info("success to register announcement");
        } catch (Exception e) {
            result.setResult("FAIL");
            logger.error("fail to register announcement.");
        }

        return result;
    }

    /**
     * 공지사항과 (이미지)파일을 같이 등록 한다.
     *
     * @param subject
     * @param content
     * @param fileUrl
     * @param thumbnailUrl
     * @param startDate
     * @param endDate
     * @param activateYn
     * @param modifyDate
     * @param registerDate
     * @param file
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/registerAnnouncementWithFile", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public PushChatAnnouncement registerAnnouncementWithFile(
            @RequestParam("subject") String subject,
            @RequestParam("content") String content,
            @RequestParam("fileUrl") String fileUrl,
            @RequestParam("thumbnailUrl") String thumbnailUrl,
            @RequestParam("startDate") String startDate,
            @RequestParam("endDate") String endDate,
            @RequestParam("activateYn") String activateYn,
            @RequestParam("modifyDate") String modifyDate,
            @RequestParam("registerDate") String registerDate,
            @RequestParam("file")MultipartFile file
    ) throws Exception {
        logger.info("registerAnnouncementWithFile");

        PushChatAnnouncement parameter = new PushChatAnnouncement();

        parameter.setSubject(subject);
        parameter.setContent(content);
        parameter.setFileUrl(fileUrl);
        parameter.setThumbnailUrl(thumbnailUrl);
        parameter.setStartDate(startDate);
        parameter.setEndDate(endDate);
        parameter.setActivateYn(activateYn);
        parameter.setModifyDate(modifyDate);
        parameter.setRegisterDate(registerDate);

        PushChatAnnouncement result = new PushChatAnnouncement();

        try {
            if(!file.isEmpty()) {
                // 파일 경로를 다시 설정 한다.
                String uploadAnnounceFilePath = this.httpServletRequest.getSession().getServletContext().getRealPath("/") + uploadAnnouncementFileDirectory;
                parameter.setFileUrl(uplaodAnnouncementFileUrl + file.getOriginalFilename());

                // 썸네일 경로를 다시 설정 한다.
                String uploadAnnounceThumbnailPath = this.httpServletRequest.getSession().getServletContext().getRealPath("/") + uploadAnnouncementThumbnailDirectory;
                parameter.setThumbnailUrl(uploadAnnouncementThumbnailUrl + file.getOriginalFilename());

                // 공지사항을 등록 한다.
                this.pushChatAnnouncementService.registerAnnouncementWithFile(parameter, uploadAnnounceFilePath, uploadAnnounceThumbnailPath, file);
            } else {
                result.setResult("FAIL");
                logger.info("fail to register announcement.");
            }
        } catch (Exception e) {
            result.setResult("FAIL");
            logger.error("fail to register announcement.");
        }

        return result;
    }

    /**
     * 공지사항 목록을 가져 온다.
     *
     * @param pushChatAnnouncement
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/selectAnnouncementList", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public List<PushChatAnnouncement> selectAnnouncementList(@RequestBody final PushChatAnnouncement pushChatAnnouncement) throws Exception {
        logger.info("selectAnnouncementList");
        return this.pushChatAnnouncementService.selectAnnouncementList(pushChatAnnouncement);
    }

    /**
     * 공지사항 상세내용을 가져 온다.
     *
     * @param pushChatAnnouncement
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/selectAnnouncementInformation", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public PushChatAnnouncement selectAnnouncementInformation(@RequestBody final PushChatAnnouncement pushChatAnnouncement) throws Exception {
        logger.info("selectAnnouncementInformation");
        return this.selectAnnouncementInformation(pushChatAnnouncement);
    }

    /**
     * 공지사항을 수정 한다.
     *
     * @param pushChatAnnouncement
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/updateAnnouncement", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public PushChatAnnouncement updateAnnouncement(@RequestBody final PushChatAnnouncement pushChatAnnouncement) throws Exception {
        logger.info("updateAnnouncement");

        PushChatAnnouncement result = new PushChatAnnouncement();

        try {
            this.pushChatAnnouncementService.updateAnnouncement(pushChatAnnouncement);
            result.setResult("SUCCESS");
            logger.info("success to update announcement.");
        } catch (Exception e) {
            result.setResult("FAIL");
            logger.error("fail to update announcement.");
        }

        return result;
    }

    /**
     * 공지사항과 (이미지)파일을 같이 수정 한다.
     *
     * @param subject
     * @param content
     * @param fileUrl
     * @param thumbnailUrl
     * @param startDate
     * @param endDate
     * @param activateYn
     * @param modifyDate
     * @param file
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/updateAnnouncementWithFile", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public PushChatAnnouncement updateAnnouncementWithFile(
            @RequestParam("subject") String subject,
            @RequestParam("content") String content,
            @RequestParam("fileUrl") String fileUrl,
            @RequestParam("thumbnailUrl") String thumbnailUrl,
            @RequestParam("startDate") String startDate,
            @RequestParam("endDate") String endDate,
            @RequestParam("activateYn") String activateYn,
            @RequestParam("modifyDate") String modifyDate,
            @RequestParam("file")MultipartFile file
    ) throws Exception {
        logger.info("updateAnnouncementWithFile");

        PushChatAnnouncement parameter = new PushChatAnnouncement();

        parameter.setSubject(subject);
        parameter.setContent(content);
        parameter.setFileUrl(fileUrl);
        parameter.setThumbnailUrl(thumbnailUrl);
        parameter.setStartDate(startDate);
        parameter.setEndDate(endDate);
        parameter.setActivateYn(activateYn);
        parameter.setModifyDate(modifyDate);

        PushChatAnnouncement result = new PushChatAnnouncement();

        try {
            if(!file.isEmpty()) {
                // 파일 경로를 다시 설정 한다.
                String uploadAnnounceFilePath = this.httpServletRequest.getSession().getServletContext().getRealPath("/") + uploadAnnouncementFileDirectory;
                parameter.setFileUrl(uplaodAnnouncementFileUrl + file.getOriginalFilename());

                // 썸네일 경로를 다시 설정 한다.
                String uploadAnnounceThumbnailPath = this.httpServletRequest.getSession().getServletContext().getRealPath("/") + uploadAnnouncementThumbnailDirectory;
                parameter.setThumbnailUrl(uploadAnnouncementThumbnailUrl + file.getOriginalFilename());

                // 공지사항을 수정 한다.
                this.pushChatAnnouncementService.updateAnnouncementWithFile(parameter, uploadAnnounceFilePath, uploadAnnounceThumbnailPath, file);
            } else {
                result.setResult("FAIL");
                logger.info("fail to update announcement.");
            }
        } catch (Exception e) {
            result.setResult("FAIL");
            logger.error("fail to update announcement.");
        }

        return result;
    }

    /**
     * 공지사항을 비활성화 햔다.
     *
     * @param pushChatAnnouncement
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/updateDeactivateAnnouncement", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public PushChatAnnouncement updateDeactivateAnnouncement(@RequestBody final PushChatAnnouncement pushChatAnnouncement) throws Exception {
        logger.info("updateDeactivateAnnouncement");

        PushChatAnnouncement result = new PushChatAnnouncement();

        try {
            this.pushChatAnnouncementService.updateDeactivateAnnouncement(pushChatAnnouncement);
            result.setResult("SUCCESS");
            logger.info("success to update deactivate announcement.");
        } catch (Exception e) {
            result.setResult("FAIL");
            logger.error("fail to update deactivate announcement.");
        }

        return result;
    }
}
